<template>
  <div class="app-container p-6">
    <div class="card">
      <div class="d-flex items-center mb-4">
        <div class="text-primary" style="font-weight:600;">文件版本管理</div>
        <div class="d-flex" style="gap:8px; margin-left:auto;">
          <el-input v-model="fileId" placeholder="输入文件ID" clearable style="width:300px" @keyup.enter="loadList" />
          <el-button size="small" type="primary" @click="loadList">查询</el-button>
          <el-button size="small" @click="refreshCurrent" :disabled="!fileId">当前版本</el-button>
          <el-button size="small" @click="openCreateDialog" :disabled="!fileId">创建版本</el-button>
        </div>
      </div>

      <div class="bg-panel radius-lg p-0">
        <el-table :data="list" v-loading="loading" style="width:100%">
          <el-table-column prop="versionNumber" label="版本号" width="100" />
          <el-table-column prop="isCurrent" label="当前" width="80">
            <template #default="{ row }">
              <el-tag v-if="row.isCurrent" type="success" size="small">是</el-tag>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column prop="changeLog" label="变更说明" />
          <el-table-column label="操作" width="320" fixed="right">
            <template #default="{ row }">
              <el-button text type="primary" @click="switchTo(row)">切换</el-button>
              <el-button text type="primary" @click="rollback(row)">回滚</el-button>
              <el-popconfirm title="确定删除该版本吗？" @confirm="remove(row)">
                <template #reference>
                  <el-button text type="danger">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="创建版本" width="600px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-form-item label="存储对象ID">
          <el-input v-model="form.storageObjectId" placeholder="关联的存储对象ID" />
        </el-form-item>
        <el-form-item label="变更说明">
          <el-input v-model="form.changeLog" type="textarea" :rows="2" />
        </el-form-item>
        <el-form-item label="标签">
          <el-input v-model="form.versionTags" placeholder="例如: v1.0,稳定" />
        </el-form-item>
        <el-form-item label="设为当前">
          <el-switch v-model="form.setAsCurrent" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitCreate">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { versionApi, type FileVersionVO } from '@/api/version'

const fileId = ref('')
const list = ref<FileVersionVO[]>([])
const loading = ref(false)

const dialogVisible = ref(false)
const form = ref({
  storageObjectId: '',
  changeLog: '',
  versionTags: '',
  setAsCurrent: true
})

const loadList = async () => {
  if (!fileId.value) return
  loading.value = true
  try {
    const { data } = await versionApi.listByFile(fileId.value)
    list.value = data || []
  } finally {
    loading.value = false
  }
}

const refreshCurrent = async () => {
  if (!fileId.value) return
  const { data } = await versionApi.current(fileId.value)
  if (data) {
    ElMessage.success(`当前版本: ${data.versionNumber}`)
  }
}

const openCreateDialog = () => {
  dialogVisible.value = true
  form.value = { storageObjectId: '', changeLog: '', versionTags: '', setAsCurrent: true }
}

const submitCreate = async () => {
  try {
    await versionApi.create({ fileId: fileId.value, ...form.value })
    ElMessage.success('创建成功')
    dialogVisible.value = false
    loadList()
  } catch (e: any) {
    ElMessage.error(e?.message || '创建失败')
  }
}

const switchTo = async (row: FileVersionVO) => {
  try {
    await versionApi.switchTo({ fileId: row.fileId, versionId: row.id })
    ElMessage.success('已切换')
    loadList()
  } catch (e: any) {
    ElMessage.error(e?.message || '切换失败')
  }
}

const rollback = async (row: FileVersionVO) => {
  try {
    await versionApi.rollback({ fileId: row.fileId, versionNumber: row.versionNumber, changeLog: '回滚' })
    ElMessage.success('已回滚')
    loadList()
  } catch (e: any) {
    ElMessage.error(e?.message || '回滚失败')
  }
}

const remove = async (row: FileVersionVO) => {
  try {
    await versionApi.delete(row.id)
    ElMessage.success('删除成功')
    loadList()
  } catch (e: any) {
    ElMessage.error(e?.message || '删除失败')
  }
}

</script>

<style scoped>
</style>


